package com.dava.helpmywife;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.dava.helpmywife.updata.UpdateUtil;

public class Help extends Activity {
	private Button sendMessage;
	private Call call;
	private final String PREFERENCES_NAME = "userInfo";
	private SharedPreferences preferences;
	private final String tag = "SoftLog";
	private String callNumBack;

	/**
	 * 打开软件自动发送求助短息到某些人手中（自动设置） 人员与短信可以在列表中设置 是否开启本功能可以在列表中设置
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.help);
		Log.i(tag, "finish init app!");
		/**
		 * 初始化设置
		 */
		preferences = getSharedPreferences(PREFERENCES_NAME,Activity.MODE_PRIVATE);
		inits inits = new inits(Help.this);
		callNumBack=preferences.getString("callNumBack", "13301066643");
		inits.init(Help.this, callNumBack, "老公，我不舒服，快来帮我！");
		//默认号码
		UpdateUtil updateManager = new UpdateUtil(Help.this);
		updateManager.checkUpdate(false);
		call = new Call();
		// 声明打电话方法
		findViewById(R.id.btn01).setOnClickListener(btnListener);
		findViewById(R.id.btn02).setOnClickListener(btnListener);
		findViewById(R.id.btn03).setOnClickListener(btnListener);
		findViewById(R.id.btn04).setOnClickListener(btnListener);
		findViewById(R.id.btn05).setOnClickListener(btnListener);
		findViewById(R.id.btn06).setOnClickListener(btnListener);
		findViewById(R.id.btn07).setOnClickListener(btnListener);
		findViewById(R.id.btn08).setOnClickListener(btnListener);
		findViewById(R.id.btn09).setOnClickListener(btnListener);
		findViewById(R.id.btn10).setOnClickListener(btnListener);
		findViewById(R.id.btn11).setOnClickListener(btnListener);
		findViewById(R.id.btn12).setOnClickListener(btnListener);
		/**
		 * 自动创建快捷方式
		 */
		if (!inits.data.isShorIco()) {
			Launcher.createShortCut(Help.this, R.drawable.ic_launcher,R.string.app_name);
			inits.data.setShorIco(true);
			//更新快捷方式
			SharedPreferences.Editor edit = preferences.edit();
			edit.putBoolean("isShorIco", true);
			edit.commit();
			inits.data.update();
		}
	}

	/**
	 * 给按钮数组加监听
	 * 
	 * @param btn
	 * @return
	 */
	OnClickListener btnListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn01:
				call.call(Help.this, preferences.getString("btn01", callNumBack));
				break;
			case R.id.btn02:
				call.call(Help.this, preferences.getString("btn02", callNumBack));
				break;
			case R.id.btn03:
				call.call(Help.this, preferences.getString("btn03", callNumBack));
				break;
			case R.id.btn04:
				call.call(Help.this, preferences.getString("btn04", callNumBack));
				break;
			case R.id.btn05:
				call.call(Help.this, preferences.getString("btn05", callNumBack));
				break;
			case R.id.btn06:
				call.call(Help.this, preferences.getString("btn06", callNumBack));
				break;
			case R.id.btn07:
				call.call(Help.this, preferences.getString("btn07", callNumBack));
				break;
			case R.id.btn08:
				call.call(Help.this, preferences.getString("btn08", callNumBack));
				break;
			case R.id.btn09:
				call.call(Help.this, preferences.getString("btn09", callNumBack));
				break;
			case R.id.btn10:
				call.call(Help.this, preferences.getString("btn10", callNumBack));
				break;
			case R.id.btn11:
				call.call(Help.this, preferences.getString("btn11", callNumBack));
				break;
			case R.id.btn12:
				call.call(Help.this, preferences.getString("btn12", callNumBack));
				break;
			default:
				Toast.makeText(Help.this, "为识别号码", Toast.LENGTH_SHORT).show();
				break;
			}

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.help, menu);
		return true;
	}

	/**
	 * 菜单选择后出发的方法
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			Intent aboutIntent = new Intent(Help.this, About.class);
			startActivity(aboutIntent);
			return true;
		case R.id.help:
			Intent helpIntent = new Intent(Help.this, Helps.class);
			startActivity(helpIntent);
			return true;
		case R.id.action_settings:
			Intent settingsIntent = new Intent(Help.this, Settings.class);
			startActivity(settingsIntent);
			return true;
		default:
			super.onOptionsItemSelected(item);
			return true;
		}
	}
}
